package com.dailyblue.java.jdbc.pack0325.dao;

import com.dailyblue.java.jdbc.pack0325.bean.Dept;
import com.dailyblue.java.jdbc.pack0325.db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeptDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    public Dept findDeptByNo(int id) {
        String sql = "select * from dept where deptNo=?";
        try {
            conn = DBManager.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Dept dept = new Dept();
                dept.setDeptNo(rs.getInt(1));
                dept.setLoc(rs.getString(3));
                dept.setDname(rs.getString(2));
                return dept;
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
